package com.stormrage.xiangyu;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.SystemClock;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.tencent.TIMConversationType;
import com.tencent.TIMManager;
import com.tencent.TIMOfflinePushListener;
import com.tencent.TIMOfflinePushNotification;
import com.tencent.ilivesdk.ILiveSDK;
import com.tencent.qalsdk.sdk.MsfSdkUtils;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import io.dcloud.application.DCloudApplication;
import io.dcloud.common.DHInterface.IApp;

/**
 * Created by mac on 16/10/27.
 */

public class XiangyuApplication extends DCloudApplication {

    public static int appid = 1400022922;
    public static int accountType = 9955;

    @Override
    public void onCreate() {
        super.onCreate();

        x.Ext.init(DCloudApplication.self());
        x.Ext.setDebug(false);
        //ILiveSDK初始化
        ILiveSDK.getInstance().initSdk(this,appid,accountType);

        if (MsfSdkUtils.isMainProcess(this)){
            TIMManager.getInstance().setOfflinePushListener(offlinePushListener);
        }

    }

    TIMOfflinePushListener offlinePushListener = new TIMOfflinePushListener() {
        @Override
        public void handleNotification(TIMOfflinePushNotification timOfflinePushNotification) {
            if (timOfflinePushNotification.getConversationType()== TIMConversationType.C2C){
                Log.i("offline",timOfflinePushNotification.getSenderIdentifier());
            }
//            timOfflinePushNotification.doNotify(getApplicationContext(),R.drawable.icon);

            Intent intent = getApplicationContext().getPackageManager().getLaunchIntentForPackage(getPackageName());
            Notification.Builder builder = new Notification.Builder(getApplicationContext());
            builder.setContentText("您收到新的视频请求");
            builder.setSmallIcon(R.drawable.icon);
            builder.setDefaults(Notification.DEFAULT_ALL);
            builder.setVibrate(new long[] {0,300,500,700});
            builder.setSound(RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(),
                    RingtoneManager.TYPE_RINGTONE));
            builder.setContentIntent(PendingIntent.getActivity(getApplicationContext(), (int) SystemClock.uptimeMillis(), intent,PendingIntent.FLAG_CANCEL_CURRENT));
            ((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).notify(520,builder.build());
        }
    };

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
